<!-- 资源位编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改资源位':'添加资源位'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
    :close-on-click-modal="false">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="资源位名称:"
        prop="title">
        <el-input
          :maxlength="255"
          v-model="form.title"
          placeholder="请输入资源位名称"
          clearable/>
      </el-form-item>

      <el-form-item
        label="资源位icon:"
        prop="icon">
        <uploadImage :limit="1" v-model="form.icon">
        </uploadImage>
      </el-form-item>
      <el-form-item
        label="选中icon:"
        prop="select_icon">
        <uploadImage :limit="1" v-model="form.select_icon">
        </uploadImage>
      </el-form-item>
      <el-form-item
        label="版本号:"
        prop="version">
        <el-input
          :maxlength="255"
          v-model="form.version"
          placeholder="请输入版本号(如1.20)"
          clearable/>
      </el-form-item>

      <el-form-item label="资源位权重:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="权重越大排序越靠前"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item
        label="链接:"
        prop="link">
        <el-input
          :maxlength="255"
          v-model="form.link"
          placeholder="请输入链接"
          clearable/>
      </el-form-item>

      <el-form-item label="是否开启" prop="status">
        <el-radio-group
          v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>
<!--      <el-form-item label="上架平台:" prop="device">-->
<!--        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>-->
<!--        <div style="margin: 15px 0;"></div>-->
<!--        <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">-->
<!--          <el-checkbox v-for="city in cities" :label="city" :key="city">{{ city }}</el-checkbox>-->
<!--        </el-checkbox-group>-->
<!--      </el-form-item>-->


      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          v-model="form.notes"
          placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import UploadImage from "@/components/uploadImage.vue";
const cityOptions = ['ios', 'android', 'pad', 'ipad', 'pc'];

export default {
  name: 'BoxResourceMyServiceEdit',
  components: {UploadImage},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  // components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        title: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '请输入icon', trigger: 'blur'}
        ],
        link: [
          {required: true, message: '请输入链接', trigger: 'blur'}
        ],
        version: [
          {required: true, message: '请输入版本号', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请输入状态，1、启用，0、禁用', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序', trigger: 'blur'}
        ],
        notes: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      bannerOptions: [],

      checkAll: false,
      checkedCities: [],
      cities: cityOptions,
      isIndeterminate: true
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        if(this.form.version !== undefined){
          this.form.version = this.$convertNumberToVersion(this.form.version);
        }
        if (this.form.device != "" && Number(this.form.device) == 1) {
          this.checkAll = true
          this.checkedCities = this.cities
        } else if (this.form.device != 1 && this.form.device != "") {
          this.checkAll = false
          if (this.form.device === undefined) {
            this.checkAll = false
            this.checkedCities = []
          } else {
            this.form.device = this.form.device.split(',').map(Number)
            let newCheckedCities = [];
            this.form.device.forEach(item => {
              if (item === 2) {
                newCheckedCities.push('ios')
              } else if (item === 3) {
                newCheckedCities.push('android')
              } else if (item === 4) {
                newCheckedCities.push('pad')
              } else if (item === 5) {
                newCheckedCities.push('ipad')
              } else if (item === 6) {
                newCheckedCities.push('pc')
              }
              this.checkedCities = newCheckedCities
            })
          }

        } else if (this.form.device == "") {
          this.checkAll = false
          this.checkedCities = []
        }
        this.isUpdate = true;
      } else {
        // this.form = {};
        this.initForm()
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      if (this.checkAll) {
        this.form.device = 1
      } else {
        let arr = []
        this.checkedCities.forEach(item => {
          if (item == 'ios') {
            arr.push(2)
          } else if (item == 'android') {
            arr.push(3)
          } else if (item == 'pad') {
            arr.push(4)
          } else if (item == 'ipad') {
            arr.push(5)
          } else if (item == 'pc') {
            arr.push(6)
          }
        })
        this.form.device = arr.join(',')
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.version = parseInt(
            this.form.version.split(".").join(""),
            10
          );
          this.$http.post('/boxResourceMyservice/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    confirmClose() {
      this.$confirm('确定要返回吗？', '温馨提示').then(() => {
        this.updateVisible(false)
        if (!this.isUpdate) {
          this.form = {};
        }
      }).catch(() => {

      });
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? cityOptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
    },
    initForm() {
      this.checkAll = false
      this.checkedCities = [];
      this.form = {}
      return this.form
    },
  }
}
</script>

<style scoped>
</style>
